home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: I can't print the date in the format I want.
- Date: Tue, 20 Feb 96 19:24:58 GMT
- Organization: none
- Message-ID: <824844298snz@genesis.demon.co.uk>
- References: <4g5nbf$8s0@newsbf02.news.aol.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4g5nbf$8s0@newsbf02.news.aol.com>
- asciizero@aol.com "ASCII zero" writes:
-
- >Hi! It is I once again.
- >
- >I am trying to use strftime() to format the date into YY-MM-DD. The format
- >seems to be working but I am unable to actually print the formatted
- >string. It compiles just fine using gcc. What have I overlooked?
- >
- >#include <stdio.h>
- >#include <time.h>
- >
- >main()
- >{
- >
- > struct tm *ptr;
- > time_t lt;
- > char *str;
- > int debug; /* inserted for debugging purposes */
- >
- > lt = time(NULL);
- > ptr = localtime(<);
- > printf("\n The system clock shows: %s\n\n", asctime(ptr));
- > debug = strftime(str, 50, "The current date is %y-%m-%d", ptr);
-
- str is an unitialised pointer to char which here you are passing as an
- argument to strftime(). This immediately results in undefined behaviour -
- you can never sensibly access the value of an uninitalised variable.
- strftime() requires as its first argument a pointer to a buffer (an array
- of char) where it can store the resulting string. You need something like:
-
- char buf[50];
-
- ...
-
- debug = strftime(buf, sizeof buf, "The current date is %y-%m-%d", ptr);
-
- > printf("-> %i", debug); /* can we get the length of the
- >formatted string? */
-
- In printf %d and %i do the same thing. However %d is generally preferred
- for historical reasons (it s the one that pre-ANSI compilers supported) and
- for the closer correspondance to the scanf() %d rather than %i specifier.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-